package com.example.tian.securityinspection.controller;



import com.example.tian.securityinspection.base.BaseController;
import com.example.tian.securityinspection.network.RestApiClient;
import com.example.tian.securityinspection.repository.SettingManager;
import com.example.tian.securityinspection.ui.Display;
import com.google.common.base.Preconditions;
import com.squareup.haha.perflib.Main;

import javax.inject.Inject;

/**
 * 主控器
 */
public class MainController extends BaseController<MainController.MainUi, MainController.MainUiCallbacks>{

    private final RestApiClient mRestApiClient;

    private final SettingManager mSettingManager;

    private final TestController mTestController;

    /*于此处注入其它控制器*/

    @Inject
    public MainController(RestApiClient restApiClient, SettingManager settingManager, TestController testController){

        super();

        mRestApiClient = Preconditions.checkNotNull(restApiClient, "restApiClient cannot be null");

        mSettingManager = Preconditions.checkNotNull(settingManager, "settingManager cannot be null");

        mTestController = Preconditions.checkNotNull(testController, "testController cannot be null");


    }

    /*对注入的控制器进行初始化*/

    @Override
    protected void onInited() {
        super.onInited();

        mTestController.init();
    }

    /*释放控制器*/

    @Override
    protected void onSuspended() {
        super.onSuspended();

        mTestController.suspend();
    }

    /*添加显示*/
    public void attachDisplay(Display display){

        Preconditions.checkNotNull(display, "display is null");
        Preconditions.checkState(getDisplay() == null, "we currently have a display");
        setDisplay(display);
    }

    /*移除显示*/
    public void detachDisplay(Display display){
        Preconditions.checkNotNull(display, "display is null");
        Preconditions.checkState(getDisplay() == display, "display is not attached");
        setDisplay(null);
    }

    /*设置显示*/

    @Override
    public void setDisplay(Display display) {
        super.setDisplay(display);

        /*其它注入的控制也需设置*/
        //例
        //mTestController.setDisplay(display);
        mTestController.setDisplay(display);
    }

    /*构筑UI*/
    @Override
    protected synchronized void populateUi(MainUi ui) {
        super.populateUi(ui);
        /*若Ui是的接口是MainHomeUi*/
        if (ui instanceof MainHomeUi){
            populateMainHomeUi((MainHomeUi) ui);
        }
    }

    private void populateMainHomeUi(MainHomeUi ui) {
        doFetchSetting(getId(ui)); //获取应用配置
    }


    /**
     * 获取应用配置
     * @param callingId
     */
    private void doFetchSetting(final int callingId){

    }

    /**
     * 当返回键被按下时
     */
    public void onBackButtonPressed() {
        Display display = getDisplay();
        if (display != null) {
            if (!display.popTopFragmentBackStack()) {
                display.navigateUp();
            }
        }
    }



  /*************************************************************************************/
    @Override
    protected MainController.MainUiCallbacks createUiCallbacks(MainController.MainUi ui) {
        return new MainUiCallbacks() {

        };
    }

    /*在此返回其它控制器*/
    //例
    public final TestController getTestController(){return mTestController;}



    /*hoome页界面接口*/
    public interface MainHomeUi extends MainUi{}

    /*MainUi接口*/
    public interface MainUi extends BaseController.Ui<MainUiCallbacks>{}


    /*Main信息回调接口*/
    public interface MainUiCallbacks{

    }

}
